<template>
  <n-modal
    v-model:show="modelValue.toDelete"
    :mask-closable="false"
    preset="dialog"
    title="确认"
    content="你确认"
    positive-text="确认"
    negative-text="算了"
    @positive-click="onPositiveClick"
    @negative-click="modelValue.toDelete = false"
  />
</template>

<script lang="ts" setup generic="T extends object & { toEdit: boolean,toDelete:boolean,toDealRowId: number|undefined}">
import type { FieldIdData } from "@model/fieldTree";
import UseCacheFinishStageMap from "@pinias/UseCacheFinishStageMap";
import UseFieldTree from "@pinias/UseFieldTree";
import pinia from "@pinias/pinia";
import { NModal, useMessage } from "naive-ui";
import { computed } from "vue";

// 全局数据
const { getFieldTree } = UseFieldTree(pinia);
const fieldTree = getFieldTree();
const { getCacheFinishStageMap } = UseCacheFinishStageMap(pinia);
const cacheFinishStageMap = getCacheFinishStageMap();
// 父组件数据
const props = defineProps<{ modelValue: T }>();
const emit = defineEmits(["update:modelValue"]);
const modelValue = computed({
  get: () => props.modelValue,
  set: val => emit("update:modelValue", val),
});
const message = useMessage();

const getFatherField: any = (id: number | undefined, fatherFieldTree: Array<FieldIdData>) => {
  // console.log('fatherFieldTree',id,fatherFieldTree);
  for (var i = 0; i < fatherFieldTree.length; i++) {
    if (fatherFieldTree[i].id == id) {
      // console.log('fatherFieldTree',id,fatherFieldTree);
      return { i, fatherFieldTree };
    }
    const result = getFatherField(id, fatherFieldTree[i].children);
    if (!(typeof result === "undefined")) return result;
  }
  return undefined;
};
const onPositiveClick = async () => {
  const id = modelValue.value.toDealRowId;
  if (await cacheFinishStageMap.checkCanDeleteFieldId(id, fieldTree)) {
    const result = getFatherField(id, fieldTree.fieldTree);
    if (result !== undefined) result.fatherFieldTree.splice(result.i, 1)[0];
    // console.log('targetRow',targetRow);
    fieldTree.deleteFieldData(id);
    modelValue.value.toDelete = false;
    //console.log("deleteFieldData",fieldTree)
  } else message.error("存在未处理的阶段，不可删除");
};
</script>
